<template>
  	<a-modal :visible="visible" :title="'库存调整'" @ok="onOk" @cancel="onCancel" :confirm-loading="loading" width="400px">
    	<a-form-model :model="form" :rules="rules" ref="formRef">
			<a-form-model-item v-if="formData.type2 !== '饮片'" label="重量类型：" prop="operateType">
				<a-select v-model="form.weightType" placeholder="请选择重量类型">
					<a-select-option value="1">入库重量</a-select-option>
					<!-- <a-select-option value="2">记录重量</a-select-option> -->
				</a-select>
			</a-form-model-item>
      		<a-form-model-item label="库存调整类型：" prop="operate">
				<a-select v-model="form.operate" placeholder="请选择类型">
					<a-select-option value="1">正常出库</a-select-option>
					<a-select-option v-if="formData.type2=='过筛'" value="2">其他出库</a-select-option>
					<a-select-option value="3">正常入库</a-select-option>
				</a-select>
			</a-form-model-item>
			<a-form-model-item label="重量（kg）：" prop="weight">
				<a-input-number style="width: 100%" v-model="form.weight" />
			</a-form-model-item>
			<a-form-model-item :label="`${form.operate=='3' ? '入库':'出库'}时间：`" prop="outTime">
				<a-date-picker :locale="locale" valueFormat="YYYY-MM-DD" style="width: 100%" v-model="form.outTime" />
			</a-form-model-item>
			<a-form-model-item label="调整原因：">
				<a-textarea v-model="form.reason" rows="4" />
			</a-form-model-item>
    	</a-form-model>
		<template #footer>
			<a-button type="primary" @click="onOk" :loading="loading">保存</a-button>
			<a-button @click="onCancel">取消</a-button>
		</template>
  	</a-modal>
</template>

<script lang="ts">
import { Vue, Component, Prop, Watch } from "vue-property-decorator";
import nextApi from "@/api/nextApi";
import locale from "ant-design-vue/lib/date-picker/locale/zh_CN";

@Component
export default class AdjustStock extends Vue {
	@Prop({ default: false }) visible!: boolean;
	@Prop({ default: {} }) formData!: any;
	loading = false;
	locale = locale
	form = {
		weightType:'1',
		nonProdId:'',
		operate: '',
		weight: 0,
		outTime: '',
		reason: '',
		type:'',
		nowStorage:''
	}
	rules = {
		operate: [
		{ required: true, message: '类型不能为空', trigger: 'change' }
		],
		weight: [
		{ required: true, message: '重量不能为空', trigger: 'blur' }
		],
		outTime: [
		{ required: true, message: '出库时间不能为空', trigger: 'change' }
		]
	};
  	@Watch('visible',{immediate:true})
  	onVisibleChange(val:boolean){
		if(val){
		this.form = {
			weightType:'1',
			nonProdId:'',
			operate: '',
			weight: 0,
			outTime: '',
			reason: '',
			type:'',
			nowStorage:''
		}
		}
  	}
	async onOk() {
		(this.$refs.formRef as any).validate(async (valid: boolean) => {
		if (!valid) return;
		try{
			this.loading = true;
			this.form.nonProdId = this.formData.id
			this.form.type = this.formData.tabType
			//根据tabType判断nowStorage是哪个类型的结存重量字段，比如ypBalanceWeight，则nowStorage=ypBalanceWeight
			let {type} = this.form
			if(type === '饮片'){
			this.form.nowStorage = this.formData.ypBalanceWeight
			}else if(type === '收粉'){
			this.form.nowStorage = this.formData.sfBalanceWeight
			}else if(type === '过筛'){
			this.form.nowStorage = this.formData.gsBalanceWeight
			}else if(type === '总混一'){
			this.form.nowStorage = this.formData.zhyBalanceWeight
			}else if(type === '总混二'){
			this.form.nowStorage = this.formData.zheBalanceWeight
			}else if(type === '制粒'){
			this.form.nowStorage = this.formData.zlBalanceWeight
			}
			const res = await nextApi.rawProductStorageUpdate(this.form)
			this.$message.success(res.msg)
			this.loading = false;
			this.$emit('close');
		}catch(e){
			this.$message.error(String(e))
			this.loading = false;
		}
		});
	}
	onCancel() {
		this.$emit('close');
		this.form = {
			weightType:'1',
			nonProdId:'',
			operate: '',
			weight: 0,
			outTime: '',
			reason: '',
			type:'',
			nowStorage:''
		}
	}
}
</script>

<style scoped>
	.a-form-item {
		margin-bottom: 18px;
	}
</style> 